<template>
    <div class="mod-config">
        <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
            <el-form-item>
                <el-input v-model="dataForm.key" placeholder="搜索公告" clearable></el-input>
            </el-form-item>
            <el-form-item>
                <el-button icon="el-icon-search" @click="getDataList()">查询</el-button>
                <el-button type="primary" icon="el-icon-circle-plus-outline" @click="addOrUpdateHandle()">新增
                </el-button>
                <el-button type="danger" icon="el-icon-delete" @click="deleteHandle()"
                           :disabled="dataListSelections.length <= 0">批量删除
                </el-button>
            </el-form-item>
        </el-form>
        <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle"
                  style="width: 100%;">
            <el-table-column type="selection" header-align="center" align="center" width="50">
            </el-table-column>
            <el-table-column type="index" header-align="center" align="center" width="100" label="序号">
                <template #default="scope">
                    <span>{{ (pageIndex - 1) * pageSize + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
            <el-table-column prop="senderId" header-align="center" align="center"
                             label="发送者编号，通常为系统管理员">
            </el-table-column>
            <el-table-column prop="title" header-align="center" align="center" label="公告标题">
            </el-table-column>
            <el-table-column prop="content" header-align="center" align="center" label="公告内容">
            </el-table-column>
            <el-table-column prop="createTime" header-align="center" align="center" label="发布时间">
                <template slot-scope="scope">
                    {{ dayjs(scope.row.createTime).format('YYYY-MM-DD HH:mm:ss') }}
                </template>
            </el-table-column>
            <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
                <template slot-scope="scope">
                    <el-button type="danger" size="small" icon="el-icon-delete" @click="deleteHandle(scope.row.id)">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
			align="right"
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :current-page="pageIndex"
            :page-sizes="[3,10, 20, 50, 100]"
            :page-size="pageSize"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
        ></el-pagination>
        <!-- 弹窗, 新增 / 修改 -->
        <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </div>
</template>

<script>
import AddOrUpdate from './announce-add-or-update.vue'
import dayjs from "dayjs";

export default {
    data() {
        return {
            dataForm: {
                key: ''
            },
            dataList: [],
            pageIndex: 1, // 当前页码
            pageSize: 10, // 每页显示条数
            total: 0, // 总条数
            dataListLoading: false,
            dataListSelections: [],
            addOrUpdateVisible: false
        }
    },
    components: {
        AddOrUpdate
    },
    mounted() {
        this.getDataList();
    },
    methods: {
        dayjs,
        // 获取数据列表
        getDataList() {
            this.dataListLoading = true;
            let data = {
                'page': this.pageIndex,
                'pageSize': this.pageSize,
                'title': this.dataForm.key
            };
            this.$http.post("/announce/searchAllAnnounce", data).then(resp => {
                if (resp.data.code === 200){
                    console.log(resp);
                    console.log(data.page, data.pageSize);
                    this.dataList = resp.data.list;
                    this.total = resp.data.totalCount;
                }else {
                    this.dataList = [];
                    this.total = 0;
                }
                this.dataListLoading = false;
            });
        },
        // 当页码变化时触发，用于分页查询数据
        currentChangeHandle(newPage) {
            this.pageIndex = newPage;
            this.getDataList(); // 重新查询数据
        },
        // 当每页显示条数变化时触发，用于分页查询数据
        sizeChangeHandle(newSize) {
            this.pageSize = newSize;
            this.pageIndex = 1; // 回到第一页
            this.getDataList(); // 重新查询数据
        },
        // 多选
        selectionChangeHandle(val) {
            this.dataListSelections = val
        },
        // 新增 / 修改
        addOrUpdateHandle(id) {
            this.addOrUpdateVisible = true
            this.$nextTick(() => {
                this.$refs.addOrUpdate.init(id)
            })
        },
        // 删除 / 批量删除
        deleteHandle(id) {
            var ids = id ? [id] : this.dataListSelections.map(item => {
                return item.id
            })
            this.$confirm(`确定对选中的公告进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http.post('/announce/deleteById', {ids: ids}).then(resp => {
                    if (resp.data.code === 200) {
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                            duration: 1500,
                        });
                        this.getDataList();
                    } else {
                        this.$message.error(resp.data.msg)
                    }
                })
            }).catch(error =>{

            });
        }
    }
}
</script>